package twitterfeed;

import java.sql.Connection;
import java.util.ArrayList;

import properties.Properties;
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
import twitterfeed.languages.Language;
import database.models.MessageModel;
import database.models.MessageOwnerModel;
import database.models.TagModel;
import database.models.TweetModel;

public class TweetStream {

	private TweetModel tweetModel;
	private ArrayList<String> list = new ArrayList<String>();

	public TweetStream(Connection connection) {
		MessageOwnerModel messageOwnerModel = new MessageOwnerModel(connection);
		MessageModel messageModel = new MessageModel(connection, messageOwnerModel);
		tweetModel = new TweetModel(
				connection, 
				messageModel,
				messageOwnerModel, 
				new TagModel(connection, messageModel));
	}

	StatusListener listener = new StatusListener() {
		/**
		 * When the listener gets a Tweet the onStatus function is called. This
		 * function is used to get the information of the Tweet in the form of a
		 * Status This function will also insert the Tweet into the database
		 * 
		 * @param Status
		 *            status
		 */
		@Override
		public void onStatus(Status status) {
			tweetModel.save(status);
			System.out.println(status.getUser().getScreenName() + ": " + status.getText());
		}

		/**
		 * Function that must be overriden but are not interesting for the
		 * application
		 */
		@Override
		public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
		}

		/**
		 * Function that must be overriden but are not interesting for the
		 * application
		 */
		@Override
		public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
		}

		/**
		 * Function that must be overriden but are not interesting for the
		 * application
		 */
		@Override
		public void onScrubGeo(long userId, long upToStatusId) {
		}

		/**
		 * Function to handle a exception
		 * 
		 * @param Exception
		 *            ex
		 */
		@Override
		public void onException(Exception ex) {
			ex.printStackTrace();
		}
	};

	/**
	 * Starts the TwitterStream
	 */
	public void startNow() {
		list = configLanguage();
		ConfigurationBuilder cb = new ConfigurationBuilder();
		//TODO: setDebugEnabled has to be switched off on a life server.
		cb.setDebugEnabled(true).setOAuthConsumerKey(Properties.getProperties().getConsumerKey())
				.setOAuthConsumerSecret(Properties.getProperties().getConsumerSecret())
				.setOAuthAccessToken(Properties.getProperties().getAccessToken())
				.setOAuthAccessTokenSecret(Properties.getProperties().getAccessTokenSecret());

		TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
				.getInstance();
		twitterStream.addListener(listener);

		String[] trackArray = list.toArray(new String[list.size()]);
		twitterStream.filter(new FilterQuery(0, null, trackArray));
	}

	/**
	 * Returns a arraylist containing words typical for an defined language.
	 * This language is defined in the Constants class Default language is Dutch
	 * 
	 * @return an ArrayList containing
	 */
	public ArrayList<String> configLanguage() {
		return Language.getLanguage().getTypicalWords();
	}
}
